package Controller;

import Service.imp.UserServiceImp;
import com.mysql.cj.Session;
import common.framework.simplemvc.RequestMapping;
import common.utils.JSONUtil;
import common.utils.Result;
import entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;


//组员写代码
@RequestMapping("/api/user")
public class UserController {
    UserServiceImp userserviceimp = new UserServiceImp();
    @RequestMapping("/login.do")
    public void login(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user=userserviceimp.login(username,password);
        if(user!=null){
            HttpSession session = req.getSession();
            session.setAttribute("user", user);
            JSONUtil.printByJSON(resp, Result.success("登录成功"));
        }else {
            JSONUtil.printByJSON(resp,Result.fail("登录失败"));
        }

    }
    @RequestMapping("/register.do")
    public void register(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String status = "ordinary";
        if (userserviceimp.register(username, password, status) > 0) {
            JSONUtil.printByJSON(resp,Result.success("注册成功"));
        }else {
            JSONUtil.printByJSON(resp,Result.fail("注册失败"));
        }
    }
    @RequestMapping("/list.do")
    public void list(HttpServletRequest req,HttpServletResponse resp){
        String username = req.getParameter("username");
        User user=userserviceimp.list(username);
        JSONUtil.printByJSON(resp,Result.success(user));
    }
    //ycy
    @RequestMapping("/userInfo.do")
    public void userInfo(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        JSONUtil.printByJSON(resp, Result.success(user));
    }
}
